package 剑指offer;

public class p53_2统计一个数字在排序数组中出现的次数 {
    public int search(int[] nums, int target) {
        int i=0;
        int j=nums.length-1;

        //寻找右边界
        while (i<=j) {
            int m=(i+j)/2;
            if (nums[m]<=target) {
                i=m+1;
            }else {
                j=m-1;
            }
        }
        int right=i;

        if (j>0 && nums[j]<target) {
            //该数组中没有target
            return 0;
        }

        //寻找左边界
        i=0;
        j=nums.length-1;
        while(i<=j) {
            int m=(i+j)/2;
            if (nums[m]<target) {
                i=m+1;
            }else {
                j = m - 1;
            }
        }
        int left=j;

        return right-left-1;

    }
}
